// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import 'element-ui/lib/theme-chalk/index.css';
import App from './App'
import router from './router'
import axios from 'axios'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

import VueQuillEditor from 'vue-quill-editor'
import echarts from 'echarts'
import CountDown from 'vue2-countdown'
Vue.prototype.$echarts = echarts
axios.defaults.baseURL='http://localhost:8081';
import BaiduMap from 'vue-baidu-map'

Vue.use(BaiduMap, {
  ak: 'GzZut9iy0kRbMTmlraPyUocXMeDMMa2o'
})
import global_ from './global'//全局变量引用
Vue.prototype.GLOBAL = global_//挂载到Vue实例上
Vue.use(ElementUI);
Vue.config.productionTip = false
Vue.use(VueQuillEditor, /* { default global options } */)
window.bus2 = new Vue();
window.bus  = new Vue();
window.bus  = new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App }
})

let  socket

Vue.prototype. init=function () {
  if(typeof(WebSocket) === "undefined"){
    alert("您的浏览器不支持socket")
  }else{
    // 实例化socket
    socket = new WebSocket(this.path)
    // 监听socket连接
    socket.onopen = this.open
    // 监听socket错误信息
    socket.onerror = this.error
    // 监听socket消息
    socket.onmessage = this.getMessage


  }
},
  Vue.prototype.open= function () {
    console.log("socket连接成功")

    socket.send("fangz")

  },
  Vue.prototype.error= function () {
    console.log("连接错误")
  },
  Vue.prototype. getMessage= function (msg) {
    console.log(msg.data)
    // this.GLOBAL.setMessage("new")
    window.sessionStorage.setItem("massage","new")
    var ff=  window.sessionStorage.getItem("massage")
    console.log(ff)
    this.$notify({
      title: '成功',
      message: '你收到一条信息',
      type: 'success',
      duration:2000
    });

  },

  Vue.prototype. send= function () {
    // console.log(this.massage)
     var kk=JSON.stringify(this.massage)
    socket.send(kk);
  }
Vue.prototype.close= function () {
  console.log("socket已经关闭")
}



router.beforeEach((to, from, next) => {
  if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
    var data = JSON.parse(window.sessionStorage.getItem("user"));

    if (data!=null) { // 查询本地存储信息是否已经登陆
      next();
    } else {
      next({
        path: '/Login', // 未登录则跳转至login页面
      });
    }
  } else {
    next();
  }
});

// 请求拦截器
axios.interceptors.request.use(config => {
  config.headers.token =  window.localStorage.getItem("token")
  return config
}, error => {
  console.log('加载超时');
  return Promise.reject(error)
});

// http响应拦截器
axios.interceptors.response.use(response => {
//没有token重新登陆
  if(response.data.states==888){
    window.localStorage.removeItem("token")
    window.localStorage.setItem("token",response.data.detail)
    window.sessionStorage.removeItem("user")
    window.sessionStorage.setItem("user", JSON.stringify(response.data.user))
    window.sessionStorage.removeItem("massage")
    // window.sessionStorage.setItem("massage","new")
    return response

  }else if(response.data.states==1000){
    window.localStorage.removeItem("user")
    window.bus.$router.replace({ name: 'Login'})
  }else{
    return response
  }
}, error => {
  console.log('加载失败')
  return (error)
})

export default axios
window.axios = require('axios');


Vue.use(ElementUI);
// Vue.use(Element, { size: 'small', zIndex: 3000 });
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
